package top.fangw.common.flyway.util;

import cn.hutool.core.util.StrUtil;

/**
 * @description: url 移除数据库名称
 * @author: fgw
 * @createDate: 2024/1/11
 */
public class UrlFormatUtil {
    public static String formatUrl(String url) {
        return generateUrl(url, "/?");
    }
    public static String formatTableUrl(String url) {

        return generateUrl(url, "/%s?");
    }
    private static String generateUrl(String url, String symbol) {
        String[] split = url.split("\\?");
        String s3 = split[1];
        String[] split1 = split[0].split("/", 4);
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < split1.length - 1; i++) {
            if (StrUtil.isNotBlank(split1[i])) {
                if (split1[i].endsWith(":")) {
                    buffer.append(split1[i]).append("//");
                } else {
                    buffer.append(split1[i]);
                }
            }
        }
        buffer.append(symbol).append(s3);
        return buffer.toString();
    }
}
